package zmz.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import zmz.entity.Student;
import zmz.entity.User;
import zmz.service.StudentService;
import zmz.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private StudentService studentService;



    @RequestMapping("/test/{id}/{name}")
    public String testUser(ModelMap modelMap, @PathVariable Integer id, @PathVariable String name){
        User usr = new User();
        usr.setId(id);
        usr.setName(name);
        modelMap.addAttribute("user", usr);
        return "user";
    }

    @RequestMapping("/demo")
    public String demo(HttpServletRequest request){
        String name = request.getParameter("xingming");
        String desc = request.getParameter("desc");
        System.out.println(name);
        System.out.println(desc);
        return "success";
    }


    @RequestMapping("/testJson")
    @ResponseBody
    public User testJson() {  //把对象转Json输出到前台页面
        User u1 = new User();
        u1.setId(3);
        u1.setName("Jack Ma");
        return u1;
    }

    @RequestMapping("/form")
    public String form() {
        return "demo";
    }

    @RequestMapping("/test2")
    public String test2(@RequestBody User user){  //接收前台页面传过来的Json数据封装成User对象


        return  "user";
    }
    @RequestMapping("/test3")
    public String test3(@RequestBody Student student){  //接收前台页面传过来的Json数据封装成User对象
        studentService.createStudent(student);

        return  "user";
    }


    @RequestMapping("/upload")
    public String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();  //文件上传
        File f = new File("/Users"+ File.separator+"luckyeez"+File.separator+name);
        file.transferTo(f);
        return "success";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        String path = "/Users"+File.separator+"luckyeez"+ File.separator+"test.png";  //文件下载
        File file = new File(path);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",path);
        httpHeaders.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.CREATED);
    }




    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }
}
